using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration;
using NUnit.Framework;
using HermesPortal.Core; 

namespace HermesPortal.NUnit
{
    [TestFixture] 
   public  class ChapterServiceFixture :ITestFixture
    {

        Chapter chapter = null;
        private int _sagaID= 0;
        private int _realmID = 0;
        private int _creatorID =0;
        private int _testStoryGuide = 0;
        private int _testPlayer = 0;
        #region ITestFixture Members
       [TestFixtureSetUp]
        public void FixtureSetUp()
        {
           _creatorID = Convert.ToInt32(ConfigurationSettings.AppSettings["TestCreator"]);
            _testStoryGuide = Convert.ToInt32(ConfigurationSettings.AppSettings["TestStoryGuide"]);
            _testPlayer = Convert.ToInt32(ConfigurationSettings.AppSettings["TestPlayer"]);
            _sagaID = Convert.ToInt32(ConfigurationSettings.AppSettings["TestSaga"]);
            _realmID = Convert.ToInt32(ConfigurationSettings.AppSettings["TestRealm"]);
          
        }
       [TestFixtureTearDown]
        public void FixtureTearDown()
        {
            
        }

        [Test]
        public void CreatChapters()
        {
            CreateTestChapters(false);
            List<Chapter> chapters =  ChapterServices.GetChapters(_sagaID,_creatorID  );
            Assert.IsNotNull(chapters);
            Assert.IsTrue(chapters.Count == 3);
            chapters[0].IsPrivate = true;
            ChapterServices.UpdateChapter(chapters[0]); 
           List<Chapter> listchapters =  ChapterServices.GetChapters(_sagaID, 0);
           Assert.IsTrue(listchapters.Count == 2);
  
        }

      
       


       #endregion

       #region helper methods
       private List<Section> CreateSections(int chapterid)
       {
           List<Section> sections = new List<Section>();  
           for (int i = 0; i < 10; i++)
           {
               Section  section = new Section();
               section.CreatorID = _creatorID;
               section.Body  = "Pearyn receives a missive one day, from his brother in walking, Casey Daphilion, the Mercere in charge of the Hibernian Tribunal.  It arranges for them to meet near the Autumnal Equinox at Newgrange, in Ireland.  Casey suggests that he has discovered a suitable place to gather Vis, and that Pearyn should be ";
               section.ViewOrder = i+1;
               section.ChapterID = chapterid;
               sections.Add(section);  
           }
           return sections; 
       }

       private void CreateTestChapters(bool createSections)
       {
           int [] chapterids= new int[3] ;  
           for (int i = 0; i < 3; i++)
           {
               chapter = new Chapter();
               chapter.SagaID = _sagaID; 
               chapter.StartMonth = 1+i;
               chapter.StartYear = 970+i;
               chapter.Title = string.Format ("This is a Test Chapter{0} Title", i);
               chapter.ViewOrder = i+1;
               chapter.IsPrivate = false;
               chapter.Header = "Test Title"+i.ToString() ;
               chapter.Footer = "Test Footer"+i.ToString() ;
               chapter.CreatorID = _creatorID;
               chapterids[i]  = ChapterServices.InsertChapter(chapter);  
           }


           if (createSections)
           {
               foreach (int chapterID in chapterids)
               {
                   List<Section> sections = CreateSections(chapterID);
                   foreach (Section section in sections)
                   {
                       ChapterServices.InsertSection(section);
                   }
               }
           }

           
       }

      

       #endregion

   }
}
